<?php

namespace App\Store\Models\OldMaterial;

use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsCraft;
use App\Store\Models\Basic\GoodsPricetype;
use App\Store\Models\Basic\GoodsStone;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Basic\Supply;
use Illuminate\Database\Eloquent\Model;

class AllotInfolist extends Model
{
    protected $table = 'site_oldmaterial_allot_infolist';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 获取列表
     */
    public function getList($request)
    {
        $query = self::where('p_id', $request['id']);

        // 筛选
        $goods_category_id = $request['goods_category_id'] ?? 0;
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }
        $goods_style_id = $request['goods_style_id'] ?? 0;
        if ($goods_style_id > 0) {
            $query->where('goods_style_id', $goods_style_id);
        }
        $audit_status = $request['audit_status'] ?? 0;
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }
        $goods_sn_code = $request['goods_sn_code'] ?? '';
        if ($goods_sn_code != '') {
            $query->where('goods_sn_code', $goods_sn_code);
        }
        $query->where('is_del', 2);
        $query->orderBy('create_at', 'desc');

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 店铺
     */
    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function startstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'start_store_id')->select('id', 'store_name');
    }

    public function targetstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'target_store_id')->select('id', 'store_name');
    }

    /**
     * 关联类别
     */
    public function goods_category(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id','category_name');
    }

    /**
     * 关联类别
     */
    public function target_goods_category(){
        return $this->hasOne(GoodsCategory::class, 'id', 'target_category_id')->select('id','category_name');
    }


    /**
     * 品牌
     */
    public function goods_brand()
    {
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id', 'brand_name');
    }

    /**
     * 供应商
     */
    public function supply()
    {
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id', 'supply_name');
    }

    /**
     * 款式
     */
    public function goods_style()
    {
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id', 'style_name');
    }

    public function goods_craft()
    {
        return $this->hasOne(GoodsCraft::class, 'id', 'goods_craft_id')->select('id', 'craft_name');
    }
    public function goods_pricetype()
    {
        return $this->hasOne(GoodsPricetype::class, 'id', 'goods_pricetype_id')->select('id', 'pricetype_name');
    }
    public function goods_stone()
    {
        return $this->hasOne(GoodsStone::class, 'id', 'goods_stone_id')->select('id', 'stone_name');
    }
    public function paytype_info()
    {
        return $this->hasOne(PaytypeList::class, 'id', 'goods_stone_id')->select('id', 'paytype_name');
    }
}
